/**
 * 
 */
package upe.dsc.algorithms.swarm.pso.process;

import java.util.Random;
import java.util.Vector;

import upe.dsc.algorithms.swarm.pso.entities.GlobalParticle;
import upe.dsc.algorithms.util.RandomDouble;

/**
 * @author ajccl/danilo
 * 
 */
public class PsoEvolucionaryImpl extends PsoInertiaImpl {

	public PsoEvolucionaryImpl(int numberOfParticles, int dimensions, int timeSteps) {
		super(numberOfParticles, dimensions, timeSteps);
	}

	private Vector<GlobalParticle> CPerturbation(Vector<GlobalParticle> swarm) {
		Vector<GlobalParticle> tmp = new Vector<GlobalParticle>();
		Random random1 = new Random();
		Random random2 = new Random();
		Random random3 = new Random();
		Random random4 = new Random();
		// double random1 = RandomDouble.getRandomDouble(1.0, 5);
		// double random2 = RandomDouble.getRandomDouble(1.0, 5);
		// double random3 = RandomDouble.getRandomDouble(1.0, 5);
		for (int i = 0; i < swarm.size(); i++) {
//			tmp.add(new GlobalParticle(this.particleType, this.particleInformationType, this.dimensions));
			tmp.add(new GlobalParticle(this.dimensions));
			for (int j = 0; j < this.dimensions; j++) {
				int p1 = random1.nextInt() % swarm.size();
				int p2 = random1.nextInt() % swarm.size();
				int p3 = random1.nextInt() % swarm.size();
				double r = random3.nextDouble();

				tmp.get(i).getPosition()[j] = swarm.get(p1).getPosition()[j] + r * (swarm.get(p2).getPosition()[j] - swarm.get(p3).getPosition()[j]);
			}
		}
		return tmp;
	}

	private Vector<GlobalParticle> MPerturbation(Vector<GlobalParticle> swarm) {
		Vector<GlobalParticle> tmp = new Vector<GlobalParticle>();
		Random random = new Random();
		// double random = RandomDouble.getRandomDouble(1.0, 5);
		for (int i = 0; i < swarm.size(); i++) {
//			tmp.add(new GlobalParticle(this.particleType, this.particleInformationType, this.dimensions));
			tmp.add(new GlobalParticle(this.dimensions));
			for (int j = 0; j < this.dimensions; j++) {
				double r = random.nextDouble();

				if (r <= (1 / this.dimensions)) {
					tmp.get(i).getPosition()[j] = RandomDouble.getRandomDouble(POSITION_MAX_VALUE, 5);
				} else {
					tmp.get(i).getPosition()[j] = swarm.get(i).getPosition()[j];
				}
			}
		}
		return tmp;
	}

}
